<template>
  <div>
    <el-form ref="cusForm" label-width="120px" size="small">
      <el-row v-for="(item, index) in formList" :key="index">
        <el-col :span="10">
          <el-form-item label="客经姓名" label-width="80px">
            <el-input  placeholder="" v-model="item.managerName"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="10">
          <el-form-item label="客经工号" label-width="80px">
            <el-input  placeholder="" v-model="item.managerNo"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="4">
          <el-button style="margin-left: 20px;" type="danger" size="mini" plain @click="deletPersonInfo(index)">删除</el-button>
        </el-col>
      </el-row>
    </el-form>
    <div class="addButton">
      <el-button type="primary" size="small" @click="addUserChild">
        <fa-icon class="fa-plus" />新增客户经理
      </el-button>
    </div>
    <el-row>
      <el-col :span="24" :offset="8">
        <el-button type="" @click="close">取消</el-button>
        <el-button type="primary" @click="submit">确定</el-button>
      </el-col>
    </el-row>
  </div>
</template>

<script>
import { mapState } from 'vuex'
export default {
  props: {
    managerList: Array
  },
  data () {
    return {
      formList: [
        {
          managerName: '',
          managerNo: ''
        }
      ]
    }
  },
  mounted () {
    if (this.managerList && this.managerList.length > 0) {
      this.formList = JSON.parse(JSON.stringify(this.managerList))
    }
  },
  methods: {
    addUserChild () {
      this.formList.push({
        managerName: '',
        managerNo: ''
      })
    },
    deletPersonInfo (index) {
      if (this.formList.length === 1) {
        this.$message({
          message: '请至少添加一名客户经理',
          type: 'warning'
        })
        return false
      } else {
        this.formList.splice(index, 1)
      }
    },
    close () {
      this.$emit('close')
    },
    submit () {
      let data = this.formList
      let res = data.filter(v => {
        return !v.managerName || !v.managerNo
      })
      if (res.length > 0) {
        this.$message({
          message: '客经姓名与工号不能为空',
          type: 'warning'
        })
        return false
      }
      console.log(data)
      this.$emit('update', data)
    }
  },
  computed: {
    ...mapState({
      dicts: state => state.common.dicts
    })
  }
}
</script>
<style lang="less">
.tag-list{
  width: 90%;
  min-height: 40px;
  border-radius: 4px;
  border: 1px solid #DCDFE6;
  list-style: none;
  display: flex;
  justify-content: flex-start;
  align-items: center;
  flex-wrap: wrap;
  box-sizing: border-box;
  padding: 5px 0;
  margin-bottom: 10px;
  cursor: pointer;
  .tag-item{
    display: inline-block;
    box-sizing: border-box;
    padding: 2px 10px;
    font-size: 12px;
    border: 1px solid #DCDFE6;
    margin: 0 5px 5px 5px;
    line-height: 22px;
    border-radius: 4px;
  }
}
.addButton {
  box-sizing: border-box;
  width: 100%;
  text-align: center;
  padding: 6px 0;
  border-top: none;
  color: #67c23a;
  cursor: pointer;
  margin: 0 auto;
}
</style>
